package org.ccg.servlet

import com.google.gson.GsonBuilder
import org.ccg.bean.AstatusBean
import java.io.BufferedReader
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class RegisterServlet : HttpServlet() {
    override fun doPost(request: HttpServletRequest, response: HttpServletResponse) {
        val jsonData: String
//        request.characterEncoding = "UTF-8"
//        response.contentType = "application/json"
//        response.characterEncoding = "UTF-8"
//        println("aaaaaaaaaaaaaa"+request.contentType)
//        var phone = ""
//        var password = ""
//        var code = ""
//
//        val mmmm = request.parameterMap
//        println("A1A2A:  " + GsonBuilder().create().toJson(mmmm))
//        val aaa = request.getParameter("username")
//        println("A3A4A:  " + aaa)
//        for ((key, value) in mmmm) {
//            println("$key = $value")
//            println(value::class)
//        }
//        val sdfsd = mmmm.keys
//        for (aaaa in sdfsd) {
//            println("$aaaa")
//            println(aaaa::class)
//        }
//        val paramNames = request.parameterNames
//        println("AAA:  " + paramNames.hasMoreElements())
//
//
//        while (paramNames.hasMoreElements()) {
//            val paramName = paramNames.nextElement() as String
//            println(paramName)
//            val paramValues = request.getParameterValues(paramName)
//            println(paramValues)
//            if (paramValues.size == 1) {
//                val paramValue = paramValues[0]
//                if ("phone" == paramName) {
//                    phone = paramValue
//                }
//                if ("password" == paramName) {
//                    password = paramValue
//                }
//                if ("code" == paramName) {
//                    code = paramValue
//                }
//            } else {
//                println("$paramName:")
//                for (i in paramValues.indices) {
//                    println("- " + paramValues[i])
//                }
//            }
//        }


        // 设置请求和响应编码
        request.characterEncoding = "UTF-8"
        response.contentType = "application/json"
        println("- --------------")
        val str = convertBufferedReaderToString(request.reader)
        println("2222222   " + str)
        if (str!=null){
            // 向客户端写JSON数据
            response.writer.print(str)
            // 清空缓冲区，结束响应
            response.writer.flush()
            response.writer.close()
        }else{

            //查询账号看看是否已经存在
            jsonData = GsonBuilder().create().toJson(AstatusBean(status = 201, msg = "注册失败", data = null))
            // 向客户端写JSON数据
            response.writer.print(jsonData)
            // 清空缓冲区，结束响应
            response.writer.flush()
            response.writer.close()
        }



    }

    private fun convertBufferedReaderToString(reader: BufferedReader): String? {
        val stringBuilder = StringBuilder()
        var line: String?
        try {
            while ((reader.readLine().also { line = it }) != null) {
                stringBuilder.append(line).append(System.lineSeparator())
            }
            return stringBuilder.toString()
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }
    }
}